 /*
 * UMLSynch - VPSDE plugin part
 * http://umlsynch.googlecode.com
 *
 * Copyright 2008-2010, Evgeny Alexeyev
 * Licensed under the EPL - v1.0
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */

package com.googlecode.umlsynch.vpsde.rpc.VPSDEDiagramManager;

import java.awt.Color;
import java.awt.Point;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Iterator;

import com.vp.plugin.ApplicationManager;
import com.vp.plugin.DiagramManager;
import com.vp.plugin.diagram.IClassDiagramUIModel;
import com.vp.plugin.diagram.IConnectorUIModel;
import com.vp.plugin.diagram.IDiagramElement;
import com.vp.plugin.diagram.IDiagramUIModel;
import com.vp.plugin.diagram.IShapeUIModel;
import com.vp.plugin.diagram.shape.IClassUIModel;
import com.vp.plugin.model.IAssociation;
import com.vp.plugin.model.IAssociationEnd;
import com.vp.plugin.model.IClass;
import com.vp.plugin.model.IConnector;
import com.vp.plugin.model.IDependency;
import com.vp.plugin.model.IGeneralization;
import com.vp.plugin.model.IModelElement;
import com.vp.plugin.model.IRealization;
import com.vp.plugin.model.IReference;
import com.vp.plugin.model.IRelationship;
import com.vp.plugin.model.IRelationshipEnd;
import com.vp.plugin.model.factory.IModelElementFactory;

public class VPClassDiagramConnectorsManager {
	private boolean IsConnectionAvailable(IDiagramElement de, 
			IDiagramElement newDiagramElement, String ConnectionType) {
		
		Iterator itr = ((IClass)de.getModelElement()).toRelationshipIterator();
		
	    while (itr.hasNext())
	    {
	    	IRelationship elem = (IRelationship)itr.next();
	    	IModelElement modelElement = elem.getFrom();
	    	
	    	if (modelElement.getName().equals(newDiagramElement.getMetaModelElement().getName())) {
	    		if (elem.getName().equals(ConnectionType))
	    			return true;
	    	}	    	
	    }   
		return false;
	}

	public  void AddConnector(IDiagramUIModel diagramModel,
							  IDiagramElement de, 
							  IDiagramElement newDiagramElement,
							  String connectorType) {
    	DiagramManager diagramManager = ApplicationManager.instance().getDiagramManager();
    	
		if (!IsConnectionAvailable(de, newDiagramElement, connectorType)) {

			IModelElement connector = null;
			String connectorDesc = null;
			if (connectorType.equals("Generalization")){

				IGeneralization gen = IModelElementFactory.instance().createGeneralization();
				gen.setName("implements");
				gen.setFrom(de.getModelElement());
				gen.setTo(newDiagramElement.getModelElement());
				connector = (IModelElement)gen;
				ApplicationManager.instance().getViewManager().showMessage("G4");
			}
			if (connectorType.equals("Bases")){
				IGeneralization gen = IModelElementFactory.instance().createGeneralization();
				gen.setName("implements");
				gen.setTo(de.getModelElement());
				gen.setFrom(newDiagramElement.getModelElement());
				connector = (IModelElement)gen; 
			}
			if (connectorType.equals("Dependency")){
				IDependency dep = IModelElementFactory.instance().createDependency();
				dep.setName("implements");
				dep.setTo(de.getModelElement());
				dep.setFrom(newDiagramElement.getModelElement());
				connector = (IModelElement)dep;				
			}
			if (connectorType.equals("Realization")){
				IRealization rel = IModelElementFactory.instance().createRealization();;
				rel.setName("implements");
				rel.setTo(de.getModelElement());
				rel.setFrom(newDiagramElement.getModelElement());
				connector = (IModelElement)rel;				
			}
			if (connectorType.equals("Association")){
				connectorDesc = IClassDiagramUIModel.SHAPETYPE_ASSOCIATION;
			}

			if (connectorType.equals("Aggregation")){
				connectorDesc = IClassDiagramUIModel.SHAPETYPE_AGGREGATION;
			}
			if (connectorType.equals("Composition")){
				connectorDesc = IClassDiagramUIModel.SHAPETYPE_COMPOSITION;
			}
			if (connectorType.equals("Containment")){
				connectorDesc = IClassDiagramUIModel.SHAPETYPE_CONTAINMENT;
			}
			
			ApplicationManager.instance().getViewManager().showMessage("G5");
			Point[] points = {new Point(de.getX(), de.getY()), 
					new Point(newDiagramElement.getX(), newDiagramElement.getY())};
			ApplicationManager.instance().getViewManager().showMessage("G6");
			if (connector != null ) {
				ApplicationManager.instance().getViewManager().showMessage("G5++");
				IDiagramElement deConnector = 
					diagramManager.createConnector(diagramModel, connector, de, newDiagramElement, points);
			} 
			else {
				if (connectorDesc != null) {

					IDiagramElement res = diagramManager.createConnector(diagramModel, connectorDesc,
							de, newDiagramElement, points);
					res.addChild((IShapeUIModel) de);
					res.addChild((IShapeUIModel) newDiagramElement);
					
					if (res instanceof IConnectorUIModel) {
						IConnectorUIModel con = (IConnectorUIModel)res;
						con.setForeground(Color.BLACK);						
						
		  				con.setFromPinType(con.PINTYPE_RATIO);
						con.setToPinType(con.PINTYPE_RATIO);
												
						ApplicationManager.instance().getViewManager().showMessage("G11");

						if (res.getModelElement() instanceof IAssociation) {

							IAssociation ass = (IAssociation) res.getModelElement();
							IRelationshipEnd fend = ass.getFromEnd();
							if (fend instanceof IAssociationEnd) {
								IAssociationEnd iacs = (IAssociationEnd)fend;
								if (connectorType.equals("Aggregation")){
									iacs.setAggregationKind(iacs.AGGREGATION_KIND_AGGREGATION);
								}
								if (connectorType.equals("Composition")){
									iacs.setAggregationKind(iacs.AGGREGATION_KIND_COMPOSITED);
								}								
							}
							ass.setName("dep");
						}
						con.setFromShape((IShapeUIModel) de);
						con.setToShape((IShapeUIModel) newDiagramElement);
						con.setSelectable(true);
						
						con.getFromConnector().setFromPinType(con.PINTYPE_RATIO);
						con.getToConnector().setFromPinType(con.PINTYPE_RATIO);
					}
				}
			}
		}
	}
}